/*
 * Copyright (C) 2010 Rodion Volkov (volkov.rodion@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.rchik.divermate;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.rchik.divermate.DiveTablesDBAdapter.DataNotPresentException;
import com.rchik.divermate.NumberPicker.OnChangedListener;

public class RestTimeActivity extends Activity implements OnChangedListener, OnClickListener {

	private int restHours;
	private int restMinutes;
	private NumberPicker hoursNumberPicker;
	private NumberPicker minutesNumberPicker;
	
	private int initialNitroLevel;
	
	private View topLayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dive_depth_time);
		
		topLayout = this.findViewById(R.id.TopLayout);
		
		hoursNumberPicker =((NumberPicker)findViewById(R.id.Depth)); 
		hoursNumberPicker.Initialize();
		minutesNumberPicker = ((NumberPicker)findViewById(R.id.Time));
		minutesNumberPicker.Initialize();
		hoursNumberPicker.setOnChangeListener(this);
		minutesNumberPicker.setOnChangeListener(this);
		hoursNumberPicker.setRange(0, 5);
		minutesNumberPicker.setRange(0, 59);
		
		((ImageView)findViewById(R.id.SubjectImageView)).setImageResource(R.drawable.rest_time_title);
		hoursNumberPicker.appendMetricType(new MetricType(R.drawable.number_picker_h, 1, 0));
		
		ImageButton restButton = (ImageButton)findViewById(R.id.DiveButton); 
		restButton.setOnClickListener(this);
		restButton.setImageResource(R.drawable.rest_button_image);
		((ImageButton)findViewById(R.id.CancelDiveButton)).setOnClickListener(this);
		
		hoursNumberPicker.setCurrent(0);
		minutesNumberPicker.setCurrent(1);
		
		initialNitroLevel = DivingMenu.getCurrentNitroLevel();
		LevelIndicator.IndicateLevel(topLayout, initialNitroLevel);
	}

	@Override
	public void onChanged(NumberPicker picker, int oldVal, int newVal) {
		switch (picker.getId())
		{
			case R.id.Depth:
			{
				restHours = newVal;
				break;
			}
			case R.id.Time:
			{
				restMinutes = newVal;
				break;
			}
			default:break;
		}
	}

	@Override
	public void onClick(View arg0) {
		
		hoursNumberPicker.clearFocus();
		minutesNumberPicker.clearFocus();
		
		switch ( arg0.getId() )
		{
			case R.id.DiveButton:
			{
				int actualMinutes = 60*restHours+restMinutes;
				
				int resultNitro = -1;
				
				try {
					resultNitro = DivingMenu.getmDbHelper().fetchTheAppropriateRestNitroLevel(
							DivingMenu.getCurrentTableId(), 
							DivingMenu.getCurrentNitroLevel(), actualMinutes);
					
				} catch (DataNotPresentException e) {
					e.printStackTrace();
					
					DatabaseErrorDialogDisplayer.displayErrorDialog(this);
					
					break;
				}
				
				DivingMenu.SetCurrentNitroLevel(resultNitro);
				
				DivingMenu.getDiveLog().add(new DiveLogElement(initialNitroLevel, 
						Integer.toString(restHours)+":"+String.format("%02d",restMinutes)));
				
				setResult(Activity.RESULT_OK);
				
				finish();
				break;
			}
			case R.id.CancelDiveButton:
			{
				setResult(Activity.RESULT_CANCELED);
				
				finish();
				break;
			}
			default: break;
		}
	}

}
